﻿using System;
using System.Collections.Generic;
using NUnit.Framework;
using Xenta.Enums;
using Xenta.Operations;
using Xenta.Utils;

namespace Xenta.Tests.Operations
{
    [TestFixture]
    public class TimeZoneTests : OperationTest
    {
        protected override IEnumerable<Type> ModelMappings
        {
            get
            {
                return NHMappings.ALL;
            }
        }

        [TestCase("xy", "name", 180)]
        public void Can_CRUD(string code,
            string name, 
            int utcOffset)
        {
            var timeZone = Processor.Execute(new CreateTimeZone
            {
                Code = code,
                Name = name,
                UtcOffset = utcOffset
            }).Result;

            Assert.NotNull(timeZone);
            timeZone.Code.AreEqualIgnoringCase(code);
            timeZone.Name.AreEqualIgnoringCase(name);
            timeZone.UtcOffset.AreEqual(utcOffset);
            timeZone.AreEqual(Processor.Execute(new GetTimeZone
            {
                EntityID = timeZone.EntityID
            }).Result);
            timeZone.AreEqual(Processor.Execute(new GetTimeZoneByCode
            {
                Code = code
            }).Result);

            code = code.Reverse();
            name = name.Reverse();
            utcOffset = utcOffset + 1;
            var upd_timeZone = Processor.Execute(new UpdateTimeZone
            {
                EntityID = timeZone.EntityID,
                Code = code,
                Name = name,
                UtcOffset = utcOffset,
                Flags = TimeZoneFlags.Active
            }).Result;

            Assert.NotNull(upd_timeZone);
            upd_timeZone.AreEqual(timeZone);
            upd_timeZone.Code.AreEqualIgnoringCase(code);
            upd_timeZone.Name.AreEqualIgnoringCase(name);
            upd_timeZone.UtcOffset.AreEqual(utcOffset);
            upd_timeZone.Flags.AreEqual(TimeZoneFlags.Active);

            Processor.Execute(new DeleteTimeZone
            {
                EntityID = timeZone.EntityID
            });
        }

        [Test]
        public void Cannot_CreateUpdate_If_Code_Empty()
        {
            Assert.Throws<ApplicationException>(() => Processor.Execute(new CreateTimeZone
            {
                Code = String.Empty
            }));
            Assert.Throws<ApplicationException>(() => Processor.Execute(new UpdateTimeZone
            {
                Code = String.Empty
            }));
        }

        [TestCase(360)]
        [TestCase(361)]
        public void Cannot_CreateUpdate_If_AbsOffset_EqOrGt_360(int utcOffset)
        {
            Assert.Throws<ApplicationException>(() => Processor.Execute(new CreateTimeZone
            {
                Code = "xy",
                UtcOffset = utcOffset
            }));
            Assert.Throws<ApplicationException>(() => Processor.Execute(new UpdateTimeZone
            {
                Code = "xy",
                UtcOffset = utcOffset
            }));
        }
    }
}
